package com.panshi.controller;

import com.panshi.service.IQuestionAuditService;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.QuestionAudit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
*问题审核
 */
@Api(tags = "问题审核业务接口")
@Slf4j
@Controller
@RequestMapping(value = "/questionAudit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class QuestionAuditController {

    @Autowired
    IQuestionAuditService questionAuditService;

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("问题审核表添加数据")
    @ApiImplicitParam(name = "questionAudit",value = "提问问题信息",required=true)
    public DataResultQA addQuestionAudit(@RequestBody QuestionAudit questionAudit){
        log.info("请求路径：{},参数：{}","/questionAudit/add",questionAudit);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = questionAudit.getQContent() == null && questionAudit.getQUserId() != 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionAuditService.addQuestionAudit(questionAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("添加完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("添加失败！");
        }
        return dataResultQA;
    }

    @PostMapping("/inquirePages")
    @ResponseBody
    @ApiOperation("分页查询问题审核表信息")
    @ApiImplicitParam(name = "pages",value = "分页查询条件,（分页大小pageSize，起始页startPage，state:{-1,1,2},user_id,question_id）",required=true)
    public DataResultQA<Pages<QuestionAudit>> inquirePagesQuestionAudit(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/questionAudit/inquirePages",pages);
        DataResultQA<Pages<QuestionAudit>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<QuestionAudit> p= questionAuditService.inquirePagesQuestionAudit(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

    @PostMapping("/inquireQuestionAuditSum")
    @ResponseBody
    @ApiOperation("查询数据条数")
    @ApiImplicitParam(name = "pages",value = "查询条件",required=true)
    public DataResultQA<Integer> inquireQuestionAuditSum(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/questionAudit/inquireQuestionAuditSum",pages);
        DataResultQA<Integer> dataResultQA = new DataResultQA();

        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        Integer sum= questionAuditService.inquireQuestionAuditSum(pages);

        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询数据条数！");
        dataResultQA.setData(sum);
        return dataResultQA;
    }

    @GetMapping("/inquireById/{id}")
    @ResponseBody
    @ApiOperation("根据id查询审核表问题")
    @ApiImplicitParam(name = "id",value = "审核表问题的id",required = true)
    public DataResultQA<QuestionAudit> inquireById(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/questionAudit/inquireById/{id}",id);
        DataResultQA<QuestionAudit> dataResultQA = new DataResultQA<>();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        QuestionAudit questionAudit= questionAuditService.inquireById(id);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据id查询！");
        dataResultQA.setData(questionAudit);
        return dataResultQA;
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation("修改问题审核表中问题信息")
    @ApiImplicitParam(name = "questionAudit",value = "提问问题信息",required = true)
    public DataResultQA updateQuestionAudit(@RequestBody QuestionAudit questionAudit){
        log.info("请求路径：{},参数：{}","/questionAudit/update",questionAudit);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = questionAudit.getQContent() == null && questionAudit.getQUserId() != 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        boolean boo = questionAuditService.updateQuestionAudit(questionAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("修改完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("修改失败！");
        }
        return dataResultQA;
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation("问题审核信息删除（标记删除）")
    @ApiImplicitParam(name = "id",value = "问题审核表id",required = true)
    public DataResultQA deleteQuestionAudit(@PathVariable  int id){
        log.info("请求路径：{},参数：{}","/questionAudit/delete/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionAuditService.deleteQuestionAudit(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("标记删除完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("标记删除失败！");
        }
        return dataResultQA;
    }

    @PostMapping("/updateState")
    @ResponseBody
    @ApiOperation("问题审核")
    @ApiImplicitParam(name = "questionAudit",value = "问题审核信息",required = true)
    public DataResultQA updateState(@Validated QuestionAudit questionAudit){
        log.info("请求路径：{},参数：{}","/questionAudit/updateState",questionAudit);
        DataResultQA dataResultQA = new DataResultQA();

        boolean b = questionAudit.getQState() == 0 || questionAudit.getCheckDescribe() == null;//审核状态不等于0,审核描述不等于null
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = questionAuditService.updateState(questionAudit);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("审核完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("审核失败！");
        }
        return dataResultQA;
    }

    //用于用户查询自己发布的问题审核
    @PostMapping("/inquireMyQuestionAudit/{id}")
    @ResponseBody
    @ApiOperation("用于用户查询自己发布的问题（根据用户id查询）")
    @ApiImplicitParam(name = "user_id",value = "用户id",required = true)
    public DataResultQA<List<QuestionAudit>> inquireMyQuestionAudit(@PathVariable int userId) {
        log.info("请求路径：{},参数：{}","/questionAudit/inquireMyQuestionAudit/{id}",userId);
        DataResultQA<List<QuestionAudit>> dataResultQA = new DataResultQA<>();
        if(userId <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        List<QuestionAudit> list = questionAuditService.inquireMyQuestionAudit(userId);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据用户id查询！");
        dataResultQA.setData(list);
        return dataResultQA;
    }
}
